<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="培训方式" prop="trainMethod">
        <el-select v-model="queryParams.trainMethod" placeholder="请选择培训方式" clearable>
          <el-option
            v-for="dict in dict.type.train_method"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="培训类型" prop="trainType">
        <el-select v-model="queryParams.trainType" placeholder="请选择培训类型" clearable>
          <el-option
            v-for="dict in dict.type.train_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资格类型" prop="qualifiedType">
        <el-select v-model="queryParams.qualifiedType" placeholder="请选择资格类型" clearable>
          <el-option
            v-for="dict in dict.type.qualified_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否审批通过" prop="isPass">
        <el-select v-model="queryParams.isPass" placeholder="请选择是否审批通过" clearable>
          <el-option
            v-for="dict in dict.type.is_pass"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->


    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange">
      <el-table-column label="培训方式" align="center" prop="trainMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.train_method" :value="scope.row.trainMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="培训类型" align="center" prop="trainType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.train_type" :value="scope.row.trainType"/>
        </template>
      </el-table-column>
      <el-table-column label="资格类型" align="center" prop="qualifiedType" />
      <el-table-column label="作业类别" align="center" prop="workKind" />
      <el-table-column label="操作项目" align="center" prop="operationType" />
      <el-table-column label="开始日期" align="center" prop="startTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="endTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">

          <el-button
            key="autoButton"
            size="mini"
            type="primary"
            style="width: 40px;"
            @click="checkInformation(scope.row)"
            v-hasPermi="['admintrain:plan:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            key="autoButton"
            type="success"
            style="width: 80px; margin-top: 10px;"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admintrain:plan:edit']"
          >添加考试地点</el-button>
          
          <el-button 
          size="mini"
          v-if="scope.row.isPass === 0"
          type="success"
          style="margin-top: 10px;" 
          v-hasPermi="['admintrain:plan:edit']"
          @click="clickSubmit(scope.row)">
            点击审核</el-button>

            <el-button
          v-if="scope.row.isPass === 1"
          disabled
          key="autoButton"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="clickSubmit(scope.row)"
            v-hasPermi="['admintrain:plan:edit']"
          >审核已通过</el-button>

        <el-button
          v-if="scope.row.isPass === -1"
          disabled
          key="autoButton"
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="clickSubmit(scope.row)"
          v-hasPermi="['admintrain:plan:edit']"
        >审批未通过</el-button>
          </template>

          
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改培训计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考试地点" prop="examPlace">
          <el-select v-model="form.examPlace" placeholder="请选择考试地点">
            <el-option
              v-for="dict in dict.type.exam_place"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


     <!--查看详细信息 -->
     <el-dialog :title="title" :visible.sync="checkInformationOpen" width="1000px" append-to-body>
      <el-form :inline="true" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="培训方式" prop="trainMethod">
          <el-select  v-model="form.trainMethod" placeholder="请选择培训方式">
            <el-option
              v-for="dict in dict.type.train_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="培训类型" prop="trainType">
          <el-select  v-model="form.trainType" placeholder="请选择培训类型">
            <el-option
              v-for="dict in dict.type.train_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资格类型" prop="qualifiedType">
          <el-select v-model="form.qualifiedType" placeholder="请选择资格类型" @change="getDictDateByPid1(form.qualifiedType)">
            <el-option
              v-for="qualified in qualifiedList"
              :key="qualified.value"
              :label="qualified.label"
              :value="qualified.value"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="作业类别" prop="workKind">
          <el-select v-model="form.workKind" placeholder="请选择作业类别" @change="getDictDateByPid2(form.workKind)">
            <el-option
              v-for="work in workKindList"
              :key="work.value"
              :label="work.label"
              :value="work.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作项目" prop="operationType">
          <el-select v-model="form.operationType" placeholder="请选择操作项目">
            <el-option
              v-for="operation in operationTypeList"
              :key="operation.value"
              :label="operation.label"
              :value="operation.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始日期" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="考试地点" prop="examPlace">
          <el-select v-model="form.examPlace" placeholder="请选择考试地点">
            <el-option
              v-for="dict in dict.type.exam_place"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="培训计划申请" prop="trainplanApplication">
          <file-upload v-model="form.trainplanApplication"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkcancel">取 消</el-button>
      </div>
    </el-dialog>


    

<el-dialog
  title="提示"
  :visible.sync="centerDialogVisible"
  width="30%"
  center>
  <span>是否确定通过审核</span>
  <span slot="footer" class="dialog-footer">
    <el-button type="success" @click="audit(1)">通 过</el-button>
    <el-button type="danger" @click="audit(-1)">不 通 过</el-button>
    <el-button @click="centerDialogVisible = false">取 消</el-button>
    
  </span>
</el-dialog>
  </div>
</template>

<script >
import { listPlan, getPlan, submitAudit, updatePlan } from "@/api/admintrain/plan";
import { getDictDateByPid } from "@/api/train/plan";

export default {
  name: "Plan",
  dicts: ['is_pass', 'qualified_type', 'operation_type', 'train_type', 'train_method', 'exam_place'],
  data() {
    return {
      qualifiedList: [],
      workKindList: [],
      operationTypeList: [],
      // 查看详细信息弹窗
      checkInformationOpen: false,
      // 点击审核数据
      trainPlan: {},
      // 审批弹框
      centerDialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainMethod: null,
        trainType: null,
        qualifiedType: null,
        operationType: null,
        isPass: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },

  watch: {
   $route(to, from) {
       window.location.reload(); //监测到路由发生跳转时刷新一次页面
   },
},
  methods: {
    // 安全资格类型标签转换
    qualifiedTypeSwap() {
      for(let i = 0; i < this.qualifiedList.length; i++) {
        if(this.qualifiedList[i].value == this.form.qualifiedType) {
          this.form.qualifiedType = this.qualifiedList[i].label;
          break;
        }
      }
    },
    // 作业类别标签类型转换
    workkindTypeSwap() {
      for(let i = 0; i < this.workKindList.length; i++) {
        if(this.workKindList[i].value == this.form.workKind) {
          this.form.workKind = this.workKindList[i].label;
          break;
        }
      };
    },
    // 操作类别类型转换
    operationTypeSwap() {
      for(let i = 0; i < this.operationTypeList.length; i++) {
        if(this.operationTypeList[i].value == this.form.operationType) {
          this.form.operationType = this.operationTypeList[i].label;
          break;
        }
      }
    },
    // 安全资格类型标签转换回来
    qualifiedTypeSwapAfter() {
      for(let i = 0; i < this.qualifiedList.length; i++) {
        if(this.qualifiedList[i].label == this.form.qualifiedType) {
          this.form.qualifiedType = this.qualifiedList[i].value;
          break;
        }
      }
    },
    // 作业类别标签类型转换
    workkindTypeSwapAfter() {
      for(let i = 0; i < this.workKindList.length; i++) {
        if(this.workKindList[i].label == this.form.workKind) {
          this.form.workKind = this.workKindList[i].value;
          break;
        }
      };
    },
    // 操作类别类型转换
    operationTypeSwapAfter() {
      for(let i = 0; i < this.operationTypeList.length; i++) {
        if(this.operationTypeList[i].label == this.form.operationType) {
          this.form.operationType = this.operationTypeList[i].value;
          break;
        }
      }
    },
    // 选择作业类别后触发
    getDictDateByPid2(pid) {
      getDictDateByPid(pid).then(response => {
        this.form.operationType = "";
        this.operationTypeList = response.rows;
      })
    },
    // 选择资格类型后触发
    getDictDateByPid1(pid) {
      getDictDateByPid(pid).then(response => {
        this.workKindList = response.rows;
        this.form.workKind = "";
        this.form.operationType = "";
        this.operationTypeList = [];
      })
    },
    // 取消查看信息弹框
    checkcancel() {
      this.checkInformationOpen = false;
    },
    // 查看信息弹窗
    checkInformation(row) {
        this.reset();
        const id = row.id || this.ids
        getPlan(id).then(response => {
          this.form = response.data;
          getDictDateByPid(0).then(response => {
            this.qualifiedList = response.rows
            this.qualifiedTypeSwap();
            })
          getDictDateByPid(this.form.qualifiedType).then(response => {
            this.workKindList = response.rows;
            this.workkindTypeSwap();
          })

          getDictDateByPid(this.form.workKind).then(response => {
            this.operationTypeList = response.rows;
            this.operationTypeSwap();
          })
          this.checkInformationOpen = true;
          this.title = "查询培训计划详细信息";
      });
      },
    // 点击审核
    clickSubmit(row) {
      this.centerDialogVisible = true;
      this.trainPlan = row;
    },
    // 审核
    audit(status) {
      this.centerDialogVisible = false;
      this.trainPlan.status = status
      submitAudit(this.trainPlan.id, status).then(response => {
        this.getList();
        this.trainPlan = [];
      });
    },
    /** 查询培训计划列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        trainMethod: null,
        trainType: null,
        qualifiedType: null,
        operationType: null,
        startTime: null,
        endTime: null,
        applyUnit: null,
        applyPeople: null,
        applyTime: null,
        passId: null,
        isPass: null,
        isDeleted: null,
        createId: null,
        updateId: null,
        createTime: null,
        updateTime: null,
        extend: null,
        isSubmit: null,
        trainplanApplication: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        getDictDateByPid(0).then(response => {
        this.qualifiedList = response.rows
        this.qualifiedTypeSwapAfter();
      })
        getDictDateByPid(this.form.qualifiedType).then(response => {
        this.workKindList = response.rows;
        this.workkindTypeSwapAfter();
      })

        getDictDateByPid(this.form.workKind).then(response => {
          this.operationTypeList = response.rows;
          this.operationTypeSwapAfter();
        })
        this.open = true;
        this.title = "修改培训计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admintrain/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>
